package com.xunshangwang.pinhaohuo.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xzp on 16/2/24.
 */
public class RadioFrameLayout extends FrameLayout implements Checkable{
    public static final String TAG="RadioFrameLayout";
    private static final int[] CHECKED_STATE_SET = {
            android.R.attr.checked
    };
    private boolean mChecked;
    private boolean mBroadcasting;
    private List<CheckBox> mCheckBoxList;
    private OnCheckedChangeListener mOnCheckedChangeListener;
    public RadioFrameLayout(Context context) {
        super(context);
        init(context,null);
    }

    public RadioFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RadioFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    public void init(Context context,AttributeSet attrs){
        final TypedArray typedArray=context.obtainStyledAttributes(attrs,CHECKED_STATE_SET);
        mChecked=typedArray.getBoolean(0,false);
        typedArray.recycle();
        this.setClickable(true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mCheckBoxList=new ArrayList<CheckBox>();
        findCheckBoxes(this);
    }
    private void findCheckBoxes(ViewGroup parent){
        int childCount=parent.getChildCount();
        for(int i=0;i<childCount;i++){
            View child=parent.getChildAt(i);
            if(child instanceof ViewGroup){
                findCheckBoxes((ViewGroup) child);
                continue;
            }
            if(child instanceof CheckBox){
                CheckBox checkBox = (CheckBox) child;
                checkBox.setEnabled(false);
                checkBox.setChecked(mChecked);
                mCheckBoxList.add(checkBox);
                continue;
            }
        }
    }
    @Override
    public void setChecked(boolean checked) {
        if(mChecked!=checked) {
            mChecked=checked;
            if(mCheckBoxList!=null){
                for(int i=0;i<mCheckBoxList.size();i++){
                    mCheckBoxList.get(i).setChecked(checked);
                }
            }
            if(mBroadcasting)
                return;
            mBroadcasting=true;
            if(mOnCheckedChangeListener!=null)
                mOnCheckedChangeListener.onCheckedChange(this, checked);
            mBroadcasting=false;
        }

    }

    @Override
    public boolean performClick() {
        toggle();
        return super.performClick();
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener){
        this.mOnCheckedChangeListener=listener;
    }
    @Override
    public void toggle() {
        if(!isChecked())
            setChecked(!mChecked);
    }
    public interface OnCheckedChangeListener{
        void onCheckedChange(RadioFrameLayout layout,boolean isChecked);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.checked = isChecked();
        return ss;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        setChecked(ss.checked);
        requestLayout();
    }

    static class SavedState extends BaseSavedState {
        boolean checked;
        SavedState(Parcelable superState) {
            super(superState);
        }
        private SavedState(Parcel in) {
            super(in);
            checked = (Boolean)in.readValue(null);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeValue(checked);
        }

        @Override
        public String toString() {
            return "RadioFrameLayout.SavedState{"
                    + Integer.toHexString(System.identityHashCode(this))
                    + " checked=" + checked + "}";
        }

        public static final Parcelable.Creator<SavedState> CREATOR
                = new Parcelable.Creator<SavedState>() {
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}
